<?php

/**
 * SKOS Converter
 * @author SilverBiology
 * @link http://www.silverbiology.com
 */

Class SKOS {
	
	public function __construct( $header = 1, $records, $url = '', $label = '', $defIndex = -1, $broader = "", $isTerm ) {
		$this->defIndex = ($defIndex) ? $defIndex : -1;
		$this->broader = $broader;
		$this->isTerm = $isTerm;
		$this->url = ($url == '') ? "http://" . $_SERVER['HTTP_HOST'] . "/" : $url;
		if ($header) {
			header("content-type: application/rdf+xml");
		}

		$this->xml = new XMLWriter();
		$this->xml->openURI('php://output');
		$this->xml->startDocument('1.0');
		$this->xml->setIndent(4);
		$this->xml->startElement('rdf:RDF');
		$this->xml->writeAttribute('xmlns:rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
		$this->xml->writeAttribute('xmlns:skos', 'http://www.w3.org/2004/02/skos/core#');

		if ($this->defIndex == -1) {
			$this->convertRecords( $records, $label );
		} else {
			foreach($records as $record) {
				if ($record->termStatus == "Primary") {
					$this->convertRecord( $record );
					continue;
				}
			}
		}

		$this->xml->endElement();
		$this->xml->endDocument();

	}
	
	private function convertRecords( $records, $label ) {
		
		$this->xml->startElement('skos:Collection');

/*
			#Broader Concept
			if ($this->broader != '') {
				$this->xml->startElement('skos:broader');		
					$this->xml->writeAttribute('rdf:resource', $this->broader);
				$this->xml->endElement();
			}
*/

			if ($this->isTerm) {
				$this->xml->writeAttribute('rdf:about', SKOS_PATH . 'term/' . urlencode($records[0]->term) );
				foreach($records as $record) {
					if ($record->termStatus == "Primary") {
						foreach($record->definitions as $def) {									
							$this->xml->startElement('skos:member');
								$this->xml->writeAttribute('rdf:resource', SKOS_PATH . 'term/' . urlencode($record->term) . "/" . $def['sequence']);
							$this->xml->endElement();
						}
						continue;
					}
				}
			} else {
				$this->xml->writeAttribute('rdf:about', SKOS_PATH . 'terms/letter/' . strtoupper(substr($records[0]->term, 0, 1)) );
				foreach( $records as $record) {
					if ($record->termStatus == "Primary") {
						$this->xml->startElement('skos:member');
							$this->xml->writeAttribute('rdf:resource', SKOS_PATH . 'term/' . urlencode($record->term));
						$this->xml->endElement();
					}
				}
			}

		$this->xml->endElement();
	}

	private function convertRecord( $record ) {
		$defs = '';
		$prefFound = false;
		$defs = $record->definitions[$this->defIndex - 1]['def'];
		$sequence = $record->definitions[$this->defIndex - 1]['sequence'];

/*		
		$i=1;
		if ($record->definitionCount > 1) {
			if (is_array($record->definitions)) {
			foreach($record->definitions as $def) {
				$defs .= $i++ . ".) " . $def['def'] . "\r\n";
			}
			}
		} else {			
			$defs = $record->definitions[0]['def'];
		}
*/

		$this->xml->startElement('skos:Concept');
		$this->xml->writeAttribute('rdf:about', $this->url . "term/" . urlencode($record->term) . "/" . $sequence);

		$this->xml->startElement('skos:inScheme');
			$this->xml->writeAttribute('rdf:resource', SKOS_PATH);
		$this->xml->endElement();

				$this->xml->writeElement('skos:definition', $defs);		
				if ($record->termStatus == 'Primary') {
					$this->xml->writeElement('skos:prefLabel', $record->term );
					$prefFound = true;
				} else {					
					$this->xml->writeElement('skos:altLabel', $record->term );
				}
				if ($record->relatedCount > 0) {
				foreach($record->related as $r) {
					if ($r->termStatus == '' && !$prefFound) {
						$this->xml->writeElement('skos:prefLabel', $r->term );
					} else {
						$this->xml->writeElement('skos:altLabel', $r->term );
					}
				}
				}

				if ($record->crossRefTerm != '') {
					$this->xml->startElement('skos:related');
					$this->xml->writeAttribute('rdf:resource', $this->url . "term/" . urlencode($record->crossRefTerm));
					$this->xml->endElement();
				}

/*
				#Broader Concept
				$this->xml->startElement('skos:broader');
					$this->xml->writeAttribute('rdf:resource', SKOS_PATH . "term/" . urlencode($record->term));
				$this->xml->endElement();
*/

		$this->xml->endElement();
	}

}
?>